﻿using ByteSite.API.Model;
using ByteSite.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using Yitter.IdGenerator;

namespace ByteSite.API.Controllers
{
    [RoutePrefix("api/message")]
    public class MessageController : ApiController
    {
        protected internal Entitys.sysconfig sysConfig;
        public MessageController()
        {
            sysConfig = new BLL.sysconfig().loadConfig();
        }

        [HttpPost]
        [Route("list")]
        public HttpResponseMessage list()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象
            MessageListRequest req = DTRequest.RequestFormParams<MessageListRequest>(request);
            BaseResponse resp = new BaseResponse();
            if (req.type <= 0)
            {
                resp.success = false;
                resp.desc = "type is error";
                var result_err = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
                };
                return result_err;
            }

            BLL.qlyc_message bll = new BLL.qlyc_message();

            List<Entitys.dt_qlyc_message> list = bll.GetList(0, x => x.delete_flag == 0 && x.type == req.type, x => x.id);
            List<Entitys.QLYCMessage> msglist = new List<Entitys.QLYCMessage>();
            foreach (Entitys.dt_qlyc_message msg in list)
            {
                if (msg.type == 2)
                {
                    //* type=2 商品价格变更
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 2,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                skuId = msg.skuid,
                                state = 1,
                                price = msg.price,
                                market_price = msg.market_price
                            }
                        });
                }
                else if (msg.type == 4)
                {
                    //* type=4 代表商品上下架变更消息
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 4,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                skuId = msg.skuid,
                                state = msg.state
                            }
                        });
                }
                else if (msg.type == 6)
                {
                    //* type=6 代表添加、删除商品池内的商品
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 6,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                skuId = msg.skuid,
                                state = msg.state
                            }
                        });
                }

                else if (msg.type == 16)
                {
                    //*type=16 商品介绍及规格参数变更消息
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 16,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                skuId = msg.skuid,
                                state = 1
                            }
                        });
                }
                else if (msg.type == 5)
                {
                    //* type=5 订单已完成
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 5,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                orderId = msg.orderId,
                                state=1
                            }
                        });
                }
                else if (msg.type == 10)
                {
                    //* type=10 代表订单取消
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 10,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                orderId = msg.orderId,
                                state = msg.state,
                                remark=msg.remark
                            }
                        });
                }

                else if (msg.type == 12)
                {
                    //* type=12 配送单生成成功消息
                    msglist.Add(
                        new Entitys.QLYCMessage
                        {
                            id = msg.id.ToString(),
                            type = 12,
                            time = Utils.ObjectToDateTime(msg.add_time).ToString("yyyy-MM-dd HH:mm:ss"),
                            result = new Entitys.QLYCMessageResult
                            {
                                orderId = msg.orderId
                            }
                        });
                }
            }

            resp.success = true;
            resp.result = msglist;

            LoggerHelper.Debug(JsonHelper2.ObjectToJSON(resp));
            var result = new HttpResponseMessage()
            {
                Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
            };
            return result;
        }

        [HttpPost]
        [Route("delete")]
        public HttpResponseMessage delete()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象
            MessageDeleteRequest req = DTRequest.RequestFormParams<MessageDeleteRequest>(request);

            LoggerHelper.Debug(JsonHelper2.ObjectToJSON(req));
            BaseResponse resp = new BaseResponse();
            if (req.messageId.IsNullOrEmpty())
            {
                resp.success = false;
                resp.desc = "messageId is null";
                var result_err = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
                };
                return result_err;
            }

            BLL.qlyc_message bll = new BLL.qlyc_message();
            int id = Utils.StrToInt(req.messageId, 0);
            if (id==0)
            {
                resp.success = false;
                resp.desc = "messageId is not a number";
                var result_err = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
                };
                return result_err;
            }
            Entitys.dt_qlyc_message msg = bll.GetModel(id);
            msg.delete_flag = 1;
            msg.delete_time = DateTime.Now;
            bool r = bll.Update(msg);
            if (!r)
            {
                resp.success = false;
                resp.desc = "系统问题，消息messageId删除失败";
                var result_err = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
                };
                return result_err;
            }


            resp.success = true;
            resp.desc = "消息messageId删除成功";

            LoggerHelper.Debug(JsonHelper2.ObjectToJSON(resp));
            var result = new HttpResponseMessage()
            {
                Content = new StringContent(JsonHelper2.ObjectToJSON(resp), Encoding.UTF8, "application/json")
            };
            return result;
        }



    }
}